package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.*;
import com.codename1.components.*;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.events.SelectionListener;
import com.codename1.ui.layouts.BorderLayout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class TabsWithSpanLabelsTest {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Custom Tabs", new BorderLayout());
            Tabs tb = new Tabs(Component.TOP) {
                @Override
                protected Component createTab(String title, Image icon) {
                    SpanButton custom = new SpanButton(title);
                    custom.setName("SpanButton");
                    custom.setIcon(icon);
                    custom.setUIID("Container");
                    custom.setTextUIID("mpiTabMetalUnSelected");
                    custom.setIconPosition(BorderLayout.NORTH);
                    return custom;
                }

                @Override
                public String getTabTitle(Component tab) {
                    return ((SpanButton) tab).getText();
                }

                @Override
                public String getTabTitle(int index) {
                    String TabTitle = "";
                    try {
                        for (int a = 0; a < getComponentCount(); a++) {
                            Container cnt = (Container) getComponentAt(a);
                            for (int b = 0; b < cnt.getComponentCount(); b++) {
                                if (cnt.getComponentAt(b).getName() != null && cnt.getComponentAt(b).getName().compareToIgnoreCase("SpanButton") == 0 && index == b) {
                                    return getTabTitle(cnt.getComponentAt(b));
                                }
                            }
                        }
                    } catch (Exception g) {
                        g.printStackTrace();
                    }
                    return TabTitle;
                }

                @Override
                public void setTabTitle(String title, Image icon, int index) {
                    try {
                        for (int a = 0; a < getComponentCount(); a++) {
                            Container cnt = (Container) getComponentAt(a);
                            for (int b = 0; b < cnt.getComponentCount(); b++) {
                                if (cnt.getComponentAt(b).getName() != null && cnt.getComponentAt(b).getName().compareToIgnoreCase("SpanButton") == 0 && index == b) {
                                    SpanButton custom = (SpanButton) cnt.getComponentAt(b);
                                    custom.setText(title);
                                    return;
                                }
                            }
                        }
                    } catch (Exception g) {
                        g.printStackTrace();
                    }
                }

                @Override
                protected void setTabSelectedIcon(Component tab, Image icon) {
                    ((SpanButton) tab).setPressedIcon(icon);
                }

                protected void selectTab(Component tab) {
                    for (int a = 0; a < this.getComponentCount(); a++) {
                        Container cmp1 = (Container) this.getComponentAt(a);
                        int selIndex = this.getSelectedIndex();
                        for (int b = 0; b < cmp1.getComponentCount(); b++) {
                            if (cmp1.getComponentAt(b).getName() != null && cmp1.getComponentAt(b).getName().startsWith("SpanButton") == true) {
                                if (selIndex == b) {
                                    ((SpanButton) tab).setTextUIID("mpiTabMetalSelected");
                                } else {
                                    ((SpanButton) cmp1.getComponentAt(b)).setTextUIID("mpiTabMetalUnSelected");
                                }
                            }
                        }
                    }
                }

                @Override
                protected void bindTabActionListener(Component tab, ActionListener l) {
                    ((SpanButton) tab).addActionListener(l);
                }
            };
            tb.addSelectionListener(new SelectionListener() {
                @Override
                public void selectionChanged(int oldSelected, int newSelected) {
                }
            });

            tb.setTabUIID(null);
            Button btn = new Button("Any Button Action");
            tb.addTab("Tab 1", btn);
            tb.addTab("Really long\ntext in tab", new Label("T2"));
            tb.addTab("Tab 3", new Label("T3"));
            tb.addTab("Tab 4", new Label("T4"));
            hi.add(BorderLayout.CENTER, tb);
            hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
